package thread;

import java.util.concurrent.*;

public class ThreadPoolTest {
    public static void main(String[] args) {
        int n = 10;
        Future<Integer>[] futures = new Future[n];
        ExecutorService pool = Executors.newFixedThreadPool(4);
        for (int i = 0; i < n; i++) {
            Future<Integer> future = pool.submit(new MyThread2(), i);
            futures[i] = future;
        }

        try {
            for (Future<Integer> future : futures) {
                System.out.println("future result :" + future.get());
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

        pool.shutdown();
    }
}

class MyThread2 implements Runnable {
    @Override
    public void run() {
        for (int i = 0; i < 2; i++) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "  i:" + i);
        }
    }
}